//
//  NovelDatabase.m
//  数据库
//
//  Created by 霍文轩 on 15/8/14.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//

// 用于执行数据库语句

#import "CollectNewsDatabase.h"
#import "DB.h"
#import "CollectNewsModel.h"

@implementation CollectNewsDatabase

+(void)insertWithModel:(CollectNewsModel *)model{
    sqlite3 * db = [DB dbOpen];
    
    NSString *str = [NSString stringWithFormat:
                     @"insert into News(c_contentid , c_title , c_thumb , c_cat_logo) values ( %ld , '%@' , '%@' , '%@') ",
                     (long)model.c_contentid,model.c_title,model.c_thumb,model.c_cat_logo];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    // 判断是否执行成功
    if(result == SQLITE_OK){
        NSLog(@"插入成功 %d",result);
    }else{
        NSLog(@"插入失败 %d",result);
    }
}


+(void)deleteWithId:(NSInteger)myId{
    sqlite3 *db = [DB dbOpen];
    NSString * str = [NSString stringWithFormat:@"delete from News where c_contentid = %ld ",(long)myId];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if(result == SQLITE_OK){
        NSLog(@"删除成功 %d",result);
    }else{
        NSLog(@"删除失败 %d",result);
    }
}

+(NSArray *)selectAll{
    sqlite3 *db = [DB dbOpen];
    sqlite3_stmt * stmt = nil;
    
    NSString * str = [NSString stringWithFormat:@"select c_contentid,c_title,c_thumb,c_cat_logo from News"];
    // 执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil); 
    
    NSMutableArray * array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            long long contentid = sqlite3_column_int64(stmt,0);
            const unsigned char * title = sqlite3_column_text(stmt, 1); // 参数分别为替身和列
            const unsigned char * thumb = sqlite3_column_text(stmt, 2);
            const unsigned char * cat_logo = sqlite3_column_text(stmt, 3);
            
            NSString * c_title = [NSString stringWithUTF8String:(const char *)title];
            NSString * c_thumb = [NSString stringWithUTF8String:(const char *)thumb];
            NSString * c_cat_logo = [NSString stringWithUTF8String:(const char *)cat_logo];
            
            CollectNewsModel *model = [CollectNewsModel collectModelWithcontentid:(NSInteger)contentid title:c_title thumb:c_thumb cat_logo:c_cat_logo];
            [array addObject:model];
        }
        
    }
    
    // 释放替身
    sqlite3_finalize(stmt);
    
    return array;
}

+(CollectNewsModel *)selectWithId:(NSInteger)myId{
    sqlite3 *db = [DB dbOpen];
    sqlite3_stmt * stmt = nil;
    
    NSString * str = [NSString stringWithFormat:@"select c_contentid,c_title,c_thumb,c_cat_logo from News where c_contentid = %ld ",(long)myId];
    // 执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            long long contentid = sqlite3_column_int64(stmt,0);
            const unsigned char * title = sqlite3_column_text(stmt, 1); // 参数分别为替身和列
            const unsigned char * thumb = sqlite3_column_text(stmt, 2);
            const unsigned char * cat_logo = sqlite3_column_text(stmt, 3);
            
            NSString * c_title = [NSString stringWithUTF8String:(const char *)title];
            NSString * c_thumb = [NSString stringWithUTF8String:(const char *)thumb];
            NSString * c_cat_logo = [NSString stringWithUTF8String:(const char *)cat_logo];
            
            CollectNewsModel *model = [CollectNewsModel collectModelWithcontentid:(NSInteger)contentid title:c_title thumb:c_thumb cat_logo:c_cat_logo];
            sqlite3_finalize(stmt);
            return model;
        }
        
    }else{
        NSLog(@"查询失败 %d",result);
    }
    
    sqlite3_finalize(stmt);
    
    return nil;
}
+(CollectNewsModel *)selectWithTitle:(NSString *)subString{
    sqlite3 *db = [DB dbOpen];
    sqlite3_stmt * stmt = nil;
    
    NSString * str = [NSString stringWithFormat:@"select c_contentid,c_title,c_thumb,c_cat_logo from News where c_title like '%%%@%%' ",subString];
    // 执行
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            long long contentid = sqlite3_column_int64(stmt,0);
            const unsigned char * title = sqlite3_column_text(stmt, 1); // 参数分别为替身和列
            const unsigned char * thumb = sqlite3_column_text(stmt, 2);
            const unsigned char * cat_logo = sqlite3_column_text(stmt, 3);
            
            NSString * c_title = [NSString stringWithUTF8String:(const char *)title];
            NSString * c_thumb = [NSString stringWithUTF8String:(const char *)thumb];
            NSString * c_cat_logo = [NSString stringWithUTF8String:(const char *)cat_logo];
            
            CollectNewsModel *model = [CollectNewsModel collectModelWithcontentid:(NSInteger)contentid title:c_title thumb:c_thumb cat_logo:c_cat_logo];
            sqlite3_finalize(stmt);
            return model;
        }
        
    }else{
        NSLog(@"查询失败 %d",result);
    }
    
    sqlite3_finalize(stmt);
    
    return nil;
}
@end